package com.rs.service.impl.rs;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.UserMapper;
import com.rs.model.CommonResult;
import com.rs.model.rs.User;
import com.rs.service.rs.UserService;
import com.rs.util.FileOperationsUtil;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper um;

	/***********APP*********/

	/**
	 *  用户注册
	 *  Optional类是Java8为了解决null值判断问题
	 *	使用Optional类可以避免显式的null值判断（null的防御性检查），
	 *	避免null导致的NPE（NullPointerException）
	 *  isPresent() 返回 true如果存在值，否则为 false 
	 */
	@Transactional
	@Override
	public CommonResult<String> userRegisterApp(User user) {
		int count = 0;//数据库影响条数
		user.setU_username(user.getU_phone());//将手机号设为账号
		switch (user.getU_sex()) {
		case "男":
			user.setU_head("C:\\CompayRs\\user\\0.jpg");//将头像设为默认的
			break;
		case "女":
			user.setU_head("C:\\CompayRs\\user\\1.jpg");//将头像设为默认的
			break;
		}

		user.setU_type(0);//默认就职状态为实习
		user.setU_examine(0);//设置审核状态为待审核
		User u = um.selectRegisterUser(user);//查询用户之前是否注册过

		boolean present = Optional.ofNullable(u).isPresent();//true:注册过   false:没注册过
		if (present) {//注册过，修改用户信息
			user.setU_uid(0);//将审批人初始化
			count = um.userChangeInfoApp(user);
		} else {//未注册过，新增用户

			count = um.userRegisterApp(user);//新增用户

		}
		if (count != 0) {//插入成功
			return new CommonResult<String>(200, "注册成功");
		} else {
			return new CommonResult<String>(200, "注册失败");
		}
	}

	/**
	 * 查看个人详情
	 */
	@Override
	public CommonResult<User> userLookInfoApp(Integer u_id) {
		User user = um.userLookInfoApp(u_id);//个人详情
		return new CommonResult<User>(200, "查询成功", user);
	}

	/**
	 * 修改密码
	 */
	@Transactional
	@Override
	public CommonResult<String> userChangePassApp(User user) {
		int count = um.userChangePassApp(user);//修改的条数
		if (count != 0) {
			return new CommonResult<String>(200, "修改成功");
		} else {

			return new CommonResult<String>(200, "修改失败");
		}
	}

	/**
	 * 修改个人信息
	 */
	@Transactional
	@Override
	public CommonResult<String> userChangeInfoApp(User user) {
		user.setU_username(user.getU_phone());//将手机号设为账号
		int count = um.userChangeInfoApp(user);
		if (count != 0) {
			return new CommonResult<String>(200, "修改成功");
		} else {

			return new CommonResult<String>(200, "修改失败");
		}
	}

	/**
	 * 修改头像
	 */
	@Transactional
	@Override
	public CommonResult<String> userHeadPortraitsApp(User user) {
		String base64 = user.getU_headBase64();//前端传的图片
		String rootPath = "C:/CompayRs/user";//上传的文件夹
		String fileType = user.getU_headType();//文件后缀
		String path = FileOperationsUtil.getFilePath(base64, rootPath, fileType);
		user.setU_head(path);//赋值头像地址
		int count = um.userHeadPortraitsApp(user);
		if (count != 0) {
			return new CommonResult<String>(200, "修改成功");
		} else {
			return new CommonResult<String>(200, "修改失败");
		}

	}

	/**
	 * 得到头像的地址
	 */
	@Override
	public CommonResult<String> userGetHeadPathApp(User user) {
		User u = um.userGetHeadPathApp(user);
		String path = u.getU_head();
		return new CommonResult<String>(200, "查询成功", path);
	}

	/**
	 * 得到用户的职位权限
	 */
	@Override
	public CommonResult<Integer> userGetRankApp(User user) {
		Integer u_rank = um.userGetRankApp(user.getU_poid());
		return new CommonResult<Integer>(200, "查询成功", u_rank);
	}

	/**
	 * 审批注册用户
	 */
	@Transactional
	@Override
	public CommonResult<String> userAuditUserApp(User user) {
		int count = um.userAuditUserApp(user);

		if (count != 0) {

			return new CommonResult<String>(200, "审批通过");
		} else {
			return new CommonResult<String>(200, "审批失败");
		}
	}

	/**
	 * 等待审核的用户列表
	 */
	@Override
	public CommonResult<List<User>> userWaitingAuditUserApp() {
		List<User> list = um.userWaitingAuditUserApp();
		return new CommonResult<List<User>>(200, "查询成功", list);
	}

	/*******pc******/
	/**
	 * pc新增用户
	 */
	@Transactional
	@Override
	public CommonResult<String> userAddEmpPc(User user) {
		String idcard = user.getU_idcard();
		user.setU_password(idcard.substring(idcard.length() - 6));
		user.setU_username(user.getU_phone());//将手机号设为账号
		user.setU_type(0);//默认就职状态为实习
		user.setU_examine(1);//设置审核状态为通过
		user.setU_entryTime(new Date());
		switch (user.getU_sex()) {
		case "男":
			user.setU_head("C:\\CompayRs\\user\\0.jpg");//将头像设为默认的
			break;
		case "女":
			user.setU_head("C:\\CompayRs\\user\\1.jpg");//将头像设为默认的
			break;
		}
		um.userRegisterApp(user);//app端的注册方法
		Integer uid = user.getU_id();//插入成功时返回的主键	

		if (uid != null) {//插入成功
			return new CommonResult<String>(200, "新增成功");
		} else {
			return new CommonResult<String>(200, "新增失败");
		}
	}

	/**
	 * 查询所有的员工
	 */
	@Override
	public CommonResult<Map<String, Object>> userQueryAllEmpPc(User user) {
		int pageNum = user.getPageNum();//当前页	
		Page<User> page = PageHelper.startPage(pageNum, 10);
		List<User> list = um.userQueryAllEmpPc();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", page.getTotal());
		map.put("pageNum", page.getPageNum());
		return new CommonResult<Map<String, Object>>(200, "查询成功", map);
	}

	/**
	 * 查询员工的个人信息
	 */
	@Override
	public CommonResult<User> userQueryOneEmpPc(Integer u_id) {
		User user = um.userQueryOneEmpPc(u_id);
		String u_uname = um.userSelectAuditName(user.getU_uid());//查询员工的审核人姓名
		user.setU_uname(u_uname);
		return new CommonResult<User>(200, "查询成功", user);
	}

	/**
	 * 修改员工信息
	 */
	@Transactional
	@Override
	public CommonResult<String> userUpdateUserInfoPc(User user) {
		int count = um.userUpdateUserInfoPc(user);
		if (count != 0) {
			return new CommonResult<String>(200, "修改成功");
		} else {

			return new CommonResult<String>(200, "修改失败");
		}
	}

	/**
	 * （姓名：模糊查询，性别，部门，职务，审核状态，就职状态）
	 */
	@Override
	public CommonResult<Map<String, Object>> userConditionQueryPc(User user) {

		Page<User> page = PageHelper.startPage(user.getPageNum(), 10);//分页
		List<User> list = um.userConditionQueryPc(user);//条件查询
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());//总条数
		mm.put("pageNum", page.getPageNum());//当前页数
		mm.put("pages", page.getPages());//总页数
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);

	}

	/**
	 * 入职时间进行条件查询
	 * writeValueAsString将对象转化成json字符串
	 * eadValue()将JSON字符串转反序列化为对象
	 * @throws ParseException 
	 */
	@Override
	public CommonResult<Map<String, Object>> userEntryTimeQueryPc(Map<String, String> map) {

		Integer pageNum = Integer.valueOf(map.get("pageNum"));//当前页

		Date startTime = TimeTypeConversionUtil.getDateByString(map.get("startTime"));//开始时间
		Date endTime = TimeTypeConversionUtil.getDateByString(map.get("endTime"));//结束时间

		Page<User> page = PageHelper.startPage(pageNum, 10);//分页
		List<User> list = um.userEntryTimeQueryPc(startTime, endTime);//list
		//map
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * 离职时间进行条件查询
	 * @throws ParseException 
	 */
	@Override
	public CommonResult<Map<String, Object>> userQuitTimeQueryPc(Map<String, String> map) {

		Integer pageNum = Integer.valueOf(map.get("pageNum"));//当前页

		Date startTime = TimeTypeConversionUtil.getDateByString(map.get("startTime"));//开始时间
		Date endTime = TimeTypeConversionUtil.getDateByString(map.get("endTime"));//结束时间

		Page<User> page = PageHelper.startPage(pageNum, 10);//分页
		List<User> list = um.userQuitTimeQueryPc(startTime, endTime);//list
		//map
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	@Override
	public User Login(User user) {

		return um.Login(user);
	}

}
